import * as THREE from 'three';
import { gsap } from 'gsap';
import { GeometryScene } from '../scenes/GeometryScene.js';
import { ParticleScene } from '../scenes/ParticleScene.js';
import { ModelScene } from '../scenes/ModelScene.js';
import { CarScene } from '../scenes/CarScene.js';

export class SceneManager {
    constructor(app) {
        this.app = app;
        this.scenes = [];
        this.currentSceneIndex = 0;
        this.isTransitioning = false;
        this.transitionDuration = 1.0;
        
        // 场景容器，用于管理多个场景
        this.sceneContainer = new THREE.Group();
        this.app.scene.add(this.sceneContainer);
    }
    
    async initScenes() {
        // 创建四个不同的场景
        const sceneConfigs = [
            { class: GeometryScene, name: '几何场景' },
            { class: ParticleScene, name: '粒子场景' },
            { class: ModelScene, name: '模型场景' },
            { class: CarScene, name: '汽车场景' }
        ];
        
        for (let i = 0; i < sceneConfigs.length; i++) {
            const config = sceneConfigs[i];
            const scene = new config.class(this.app, i);
            
            // 初始化场景
            await scene.init();
            // 添加到场景数组
            this.scenes.push(scene);
            
            // 添加到容器
            this.sceneContainer.add(scene.group);
            
            // 除了第一个场景，其他场景初始时隐藏
            if (i !== 0) {
                scene.setVisible(false);
            }
        }
        
        // 激活第一个场景
        if (this.scenes.length > 0) {
            this.scenes[0].activate();
        }
        
        console.log(`场景管理器初始化完成，共创建 ${this.scenes.length} 个场景`);
    }
    
    async switchToScene(index) {
        if (index === this.currentSceneIndex || 
            this.isTransitioning || 
            index < 0 || 
            index >= this.scenes.length) {
            return;
        }
        
        this.isTransitioning = true;
        
        const currentScene = this.scenes[this.currentSceneIndex];
        const targetScene = this.scenes[index];
        
        console.log(`切换场景: ${currentScene.name} -> ${targetScene.name}`);
        
        // 开始过渡动画
        await this.performSceneTransition(currentScene, targetScene);
        
        this.currentSceneIndex = index;
        this.isTransitioning = false;
    }
    
    async performSceneTransition(fromScene, toScene) {
        // 准备目标场景
        toScene.prepare();
        
        // 创建平滑的过渡效果
        const timeline = gsap.timeline();
        
        // 第一阶段：淡出当前场景
        timeline.to(fromScene.group.position, {
            duration: this.transitionDuration * 0.3,
            y: -2,
            ease: "power2.in"
        });
        
        timeline.to(fromScene.group.scale, {
            duration: this.transitionDuration * 0.3,
            x: 0.8,
            y: 0.8,
            z: 0.8,
            ease: "power2.in"
        }, "<");
        
        // 第二阶段：切换场景
        timeline.call(() => {
            fromScene.deactivate();
            fromScene.setVisible(false);
            
            toScene.setVisible(true);
            toScene.group.position.set(0, 2, 0);
            toScene.group.scale.set(0.8, 0.8, 0.8);
        });
        
        // 第三阶段：淡入新场景
        timeline.to(toScene.group.position, {
            duration: this.transitionDuration * 0.7,
            y: 0,
            ease: "power2.out"
        });
        
        timeline.to(toScene.group.scale, {
            duration: this.transitionDuration * 0.7,
            x: 1,
            y: 1,
            z: 1,
            ease: "power2.out"
        }, "<");
        
        timeline.call(() => {
            toScene.activate();
        });
        
        // 等待动画完成
        await new Promise(resolve => {
            timeline.eventCallback("onComplete", resolve);
        });
    }
    
    update() {
        // 更新当前活跃的场景
        if (this.scenes[this.currentSceneIndex]) {
            this.scenes[this.currentSceneIndex].update();
        }
        
        // 如果正在过渡，也更新目标场景
        if (this.isTransitioning) {
            this.scenes.forEach((scene, index) => {
                if (index !== this.currentSceneIndex && scene.isVisible) {
                    scene.update();
                }
            });
        }
    }
    
    onResize(width, height) {
        this.scenes.forEach(scene => {
            if (scene.onResize) {
                scene.onResize(width, height);
            }
        });
    }
    
    getCurrentScene() {
        return this.scenes[this.currentSceneIndex];
    }
    
    getScene(index) {
        return this.scenes[index];
    }
    
    getAllScenes() {
        return this.scenes;
    }
    
    dispose() {
        this.scenes.forEach(scene => {
            scene.dispose();
        });
        this.scenes = [];
        
        if (this.sceneContainer) {
            this.app.scene.remove(this.sceneContainer);
        }
    }
}
